package com.lc.sell.util;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * @author lc
 * @version 1.0
 * @date 2022/4/19 22:45
 */
@Component
public class AliPayUtil {

    static AlipayClient alipayClient;

    /**
     * 初始化
     */
    @PostConstruct
    public void init() {
        alipayClient = new DefaultAlipayClient("https://openapi.alipaydev.com/gateway.do",
                "2021000119650335",
                "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCQYf3o7VSRby0kCMRrrmfuw2eAaOocbTFD3qkt4PC3g1JZIIZLDo+PESKOmZGAXO6xkDX30OY09lA1zd/ac9pjiuv5QMUs7dFqagaPYx/aIKklwuoox0jWogEYA26uSywr2Wc/PZr3Zx8QLv7Da+lhEORS9CMUHlzQw0t//jPOnM5UBeWsxmVdcD94EU6y456Gr4Ib2d0BPxEZtrcP2B98dvWKd5FQrNLDtsiMoi6Okdxpjkp6ymj2u05RrLN4DE2W/IXJaGPJCr1t6Gt2TPv6u877RonVRoUlUZTgFOwLPJJrhd1tugK1iy9DYME4DLPfDjqF+HE9Q+7hFcehSIUpAgMBAAECggEAS6fdacF6z95m0/v4GMjB0z33LTVMf2rEJuE020KEQFUCwIsnc4sEtJ3w8rILqSyJMufzL5D6IAR1kUv4obqVVBXGgsxqxZk0Y8N8/oOdfCojGBNjqcyLIVSwqAmcZJ4lXjDERkIcwsj5YPyPKI2SM/eq/MDK5M7oKcg79qyYLwn3O8hGICYoDam401c3NAIISO8bwy6E2WHT/4j957KCqrJVNZmty0GHFmbbZwppXChXOxUDYLHSlOCNa2gJ8O69m0vtipvhTtAz0fojFyJv0k+VuekmTlX+Eh/50/gJcpTvyusRwm9iQpsGpkvCFHqllwQYd3o++CvqL7EwSayfAQKBgQDTYXSeT6xx48FLibAu5Gi3Ak1lP3VutUDqeT+8AFSB+uGfyit+efBeEAkIJLB9sPR1lXmxIkyi7qXGg1skE5d6fqjpnR+Bg6XLcsdWvNTz70DjxwtuSsne0s3HDlCDXII3xINttZo0jjFrahibHHjpM9zLjLU1jap8sGDXgplUyQKBgQCu3B4mX6vOosSD+CjaPt894SNzhN1+e0t/WWwMhY4rFzpYITNwxJEY2owzHm9q/xp1Okgm46NjPMXFYbGLJqMonUxwnIqD49yDVnioWrGry6Sa2MF/kjzOGXTnCfQyknUCGYdgpGXu9EjAas7yy64yDWFuK/ilZawrYaqGI1+9YQKBgGhTC/RqTCi1iJvYa7zo/Al+uGYFCNdH268dgmguHYc9JAfyBr9YPVqfPWXofba7FBvIVxQmYn5znBu6jsviY3uQcZtC8d7A853SmzFi5xMxfOwuO6UGglVX4zmgBfqfOKF/eoI2A9J2b0udRGGhjsdB0nIfkvvIXGFHx7P1f+mJAoGBAIXJduwHYS2tzcvnMGKjniz7OCOnscyXS1F5urjt6rO/hu1tIa2TSC0K8iAelkL4QmcVG6VdoGJKWktGDxCXlFh72cODkluNGOgIXjL5iV0XZ67uT0ZrPki2o4IuVH/pHMtpevIPdspYb92BGywbeQFkyXoA/LKX0x4yEMWnBRahAoGAI3BCObatffqhbaI4qUy2wxGOefqSZGGPx5HWf6wpJKzsCzS1IycDA35egn/l4TwJOUOkVHxaV1xn1T0AcXKCKe8+vgG89p2a6Xl1L2S5sYNFQrlaqdRTUsWNE5YnWaAcLIyatOiLFXOr0J6lqFehAYAVp3Qpk3Hsosx+v3omVSM=",
                "json",
                "utf-8",
                "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqGLmzbM1sgrnj60rddcEHXj4F93MTBafQdGtSBDnKPalNQq7y7GTlUJTOS403RaZVWH842iG67MXmWLK9KI/wN5r/QFoFDR8d+QTgqd1qak3GvPN2sYTKXR6wFX+z/WojlhBVXyhiYOm2sHW98js199PzioBtQmKUpUHR+/fpPfTPg5SS1ZDhnQ/O354bwTuMU2A7jE68T1lpdwDT/u5qbKwd73e1QN01ZKm59C/xsk4cIYrfAgMCMyeXKnGSgj0GOw293/IZx84eJ1Wk6zaN6B8euXzTZj4AsRl4iAaC4YnJ5lCJNzulKAQTZc88gQGIjvId5cOYr9FPwzNNyNT6QIDAQAB",
                "RSA2");
    }

    /**
     * alipay.trade.wap.pay：H5手机网站支付接口2.0（外部商户创建订单并支付）
     * @param out_trade_no：商户订单号
     * @param total_amount：支付金额，单位：元
     * @return
     */
    public static String alipayTradeWapPay(String out_trade_no, String total_amount){
        try {
            //（1）封装bizmodel信息
            AlipayTradeWapPayModel model = new AlipayTradeWapPayModel();
            //SDK已经封装掉了公共参数，这里只需要传入业务参数。以下方法为sdk的model入参方式(model和biz_content同时存在的情况下取biz_content)。
            model.setOutTradeNo(out_trade_no);
            model.setSubject("支付宝手机网站支付");
            model.setBody("支付宝手机网站支付");
            model.setProductCode("QUICK_WAP_WAY");
            model.setTotalAmount(total_amount);
            model.setTimeoutExpress("30m");
            model.setQuitUrl("http://101.43.97.236:9009/#/pages/mine/mine");
            //（2）设置请求参数
            AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();
            alipayRequest.setReturnUrl("http://101.43.97.236:9009/#/pages/mine/mine");
            alipayRequest.setNotifyUrl("http://101.43.97.236:9009/#/pages/mine/mine");
            alipayRequest.setBizModel(model);
            //（3）请求
            String form = alipayClient.pageExecute(alipayRequest).getBody();
            System.out.println("*********************\n返回结果为："+form);
            return form;
        } catch (AlipayApiException e) {
            e.printStackTrace();
            return null;
        }
    }

}
